package com.gmail.nealnb.sf;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.gmail.nealnb.sf.util.EncdecUtil;

public class SMSForwardingActivity extends Activity {

	ImageButton btStart, btStop, btExit, btHelp;
	TextView info;
	EditText toNo, fromNo;
	// help dialog
	Dialog dlg;
	LayoutInflater inflater;
	View layout;

	private void initComponent() {
		info = (TextView) findViewById(R.id.statustext);
		toNo = (EditText) findViewById(R.id.tono);
		fromNo = (EditText) findViewById(R.id.fromno);
		btExit = (ImageButton) findViewById(R.id.exitbt);
		btStart = (ImageButton) findViewById(R.id.startbt);
		btStop = (ImageButton) findViewById(R.id.stopbt);
		btHelp = (ImageButton) findViewById(R.id.helpbt);
		// init for status
		SharedPreferences settings = getSharedPreferences(
				"com.gmail.nealnb.sf", 0);
		String enable = settings.getString("enable", "0");
		String to = EncdecUtil.decrypt("miao", settings.getString("to", ""));
		String from = EncdecUtil
				.decrypt("miao", settings.getString("from", ""));
		toNo.setText(to);
		fromNo.setText(from);
		if ("0".equals(enable)) {
			info.setText("已暂停！");
		} else {
			info.setText("已启动！");
		}
		// init for help dialog
		inflater = (LayoutInflater) this
				.getSystemService(LAYOUT_INFLATER_SERVICE);
		dlg = new Dialog(SMSForwardingActivity.this, R.style.Theme_CustomDialog);
		layout = inflater.inflate(R.layout.helpdialog, null);
		layout.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				dlg.dismiss();
				return false;
			}
		});
		dlg.setOnDismissListener(new OnDismissListener() {
			@Override
			public void onDismiss(DialogInterface dialog) {
			}
		});
		dlg.setContentView(layout);
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		initComponent();
		btExit.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		btStart.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				String toNum = toNo.getText().toString();
				String fromNum = fromNo.getText().toString();
				SharedPreferences.Editor editor = getSharedPreferences(
						"com.gmail.nealnb.sf", 0).edit();
				editor.putString("from", EncdecUtil.encrypt("miao", fromNum));
				editor.putString("to", EncdecUtil.encrypt("miao", toNum));
				editor.putString("enable", "1");
				editor.commit();
				info.setText("已启动！");
			}
		});
		btStop.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				SharedPreferences.Editor editor = getSharedPreferences(
						"com.gmail.nealnb.sf", 0).edit();
				editor.putString("enable", "0");
				editor.commit();
				info.setText("已暂停！");
			}
		});
		btHelp.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dlg.show();
			}
		});
	}
}